home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / FLI106C.ZIP;1 / DIANUM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-12  |  13.3 KB  |  510 lines

  1. //
  2. // The Fusion Library Interface for DOS
  3. // Version 1.06c
  4. // Copyright (C) 1990, 1991, 1992
  5. // Software Dimensions
  6. //
  7. // Numerics
  8. //
  9.  
  10. #include "fli.h"
  11. #include "elements.h"
  12. #include "colors.h"
  13.  
  14. #ifdef __BCPLUSPLUS__
  15. #pragma hdrstop
  16. #if defined(__SMALL__) || defined(__TINY__) || defined(__MEDIUM__)
  17. #pragma option -O-e
  18. #endif
  19. #endif
  20.  
  21. #include <ctype.h>
  22. #include <string.h>
  23. #include <mem.h>
  24. #include <string.h>
  25.  
  26. Numerics::Numerics(int _NoEditErase)
  27. {
  28.   CurrentLocation=0;
  29.   NoEditErase=_NoEditErase;
  30.   EditOverriden=0;
  31.   AllowedNegative=0;
  32. }
  33.  
  34. Numerics::~Numerics()
  35. {
  36.   if (Value)
  37.     delete Value;
  38. }
  39.  
  40. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  41. //
  42. // HighLight()
  43. //
  44. // Highlight the element
  45. //
  46. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  47.  
  48. static int InsertMode=0;
  49.  
  50. void Numerics::HighLight()
  51. {
  52.   Blaze->BigCursor(InsertMode);
  53.  
  54.   MouseHide();
  55.   if (strlen(Mask)!=strlen(Value))
  56.     Blaze->CharacterRepeater(X+strlen(Value),Y,strlen(Mask)-strlen(Value),
  57.       Colors.NumHiLite,' ');
  58.   (*Blaze) (X,Y) << Colors.NumHiLite << Numerics::Value;
  59.   MouseShow();
  60.  
  61.   if (CurrentLocation>=strlen(Mask))
  62.     CurrentLocation=strlen(Mask)-1;
  63.  
  64.   Blaze->WindowGotoXY(X+CurrentLocation,Y);
  65. }
  66.  
  67. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  68. //
  69. // Show()
  70. //
  71. // Show the element
  72. //
  73. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  74.  
  75. void Numerics::Show()
  76. {
  77.   Blaze->BigCursor();
  78.  
  79.   (*Blaze) (X,Y) << ((Available()==CompleteEvent)?Colors.NumNormal:Colors.DiaDeadLocator);
  80.  
  81.   MouseHide();
  82.   TrimTrailingZeros();
  83.   MaskShow(Mask,Value,*Blaze);
  84.   TrimTrailingZeros();
  85.   EditOverriden=0;
  86.   CurrentLocation=strlen(Value);
  87.   MouseShow();
  88. }
  89.  
  90. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  91. //
  92. // EventValidation()
  93. //
  94. // Validates the number that was input by the user
  95. //
  96. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  97.  
  98. int Numerics::EventValidation(int Event)
  99. {
  100.   if (!AllowedAfterDecimal && !AllowedBeforeDecimal)
  101.     return Event;
  102.  
  103.   // ---------------------------------------------------------------
  104.   // determine current amount of numbers in string
  105.   // ---------------------------------------------------------------
  106.  
  107.   long ActualBeforeDecimal=0;
  108.  
  109.   long ActualAfterDecimal=0;
  110.  
  111.   if (AllowedBeforeDecimal)
  112.   {
  113.     if (strchr(Value,'.'))
  114.       ActualBeforeDecimal=(strchr(Value,'.')-Value)-
  115.         ((strchr(Value,'-'))?1:0);
  116.     else
  117.       ActualBeforeDecimal=strlen(Value)-((strchr(Value,'-'))?1:0);
  118.   }
  119.  
  120.   if (AllowedAfterDecimal)
  121.   {
  122.     if (strchr(Value,'.'))
  123.       ActualAfterDecimal=(((Value+strlen(Value))-1)-strchr(Value,'.'));
  124.   }
  125.  
  126.   // ---------------------------------------------------------------
  127.   // process event
  128.   // ---------------------------------------------------------------
  129.  
  130.   switch (Event)
  131.   {
  132.     case ValidatedMousedEvent:
  133.       if (MouseEvent&MouseDoubleClick)
  134.         return ClickEvent;
  135.       return Event;
  136.  
  137.     case kbIns:
  138.       EditOverriden++;
  139.       InsertMode=(InsertMode)?0:1;
  140.       Blaze->BigCursor(InsertMode);
  141.       return CompleteEvent;
  142.  
  143.     case kbHome:
  144.       EditOverriden++;
  145.       CurrentLocation=0;
  146.       return CompleteEvent;
  147.  
  148.     case kbEnd:
  149.       EditOverriden++;
  150.       CurrentLocation=strlen(Value);
  151.       return CompleteEvent;
  152.  
  153.     case kbLeft:
  154.       EditOverriden++;
  155.       if (!CurrentLocation)
  156.         return CompleteEvent;
  157.       CurrentLocation--;
  158.       return CompleteEvent;
  159.  
  160.     case kbRight:
  161.       EditOverriden++;
  162.       if (CurrentLocation==strlen(Value))
  163.         return CompleteEvent;
  164.       CurrentLocation++;
  165.       return CompleteEvent;
  166.  
  167.     case '.':
  168.       if (AllowedAfterDecimal)
  169.       {
  170.         if (NoEditErase && !EditOverriden)
  171.         {
  172.           EditOverriden++;
  173.           *(Value+1)=0;
  174.           CurrentLocation=0;
  175.           ActualBeforeDecimal=0;
  176.           ActualAfterDecimal=0;
  177.         }
  178.  
  179.         if (strchr(Value,'.'))
  180.           return CompleteEvent;
  181.  
  182.         if (!InsertMode)
  183.         {
  184.           if (!*(Value+CurrentLocation))
  185.           {
  186.             *(Value+CurrentLocation++)='.';
  187.             *(Value+CurrentLocation)=0;
  188.             return CompleteEvent;
  189.           }
  190.           else
  191.           {
  192.             if ((strlen(Value)-1)-CurrentLocation<=AllowedAfterDecimal)
  193.             {
  194.               *(Value+CurrentLocation++)='.';
  195.               *(Value+CurrentLocation)=0;
  196.               return CompleteEvent;
  197.             }
  198.           }
  199.         }
  200.         else
  201.         {
  202.           if (!*(Value+CurrentLocation))
  203.           {
  204.             *(Value+CurrentLocation++)='.';
  205.             *(Value+CurrentLocation)=0;
  206.             return CompleteEvent;
  207.           }
  208.           else
  209.           {
  210.             int ModifiedAfter=strlen(Value)-CurrentLocation;
  211.             int ModifiedBefore=CurrentLocation-((*Value=='-')?1:0);
  212.             if (ModifiedAfter<=AllowedAfterDecimal &&
  213.               ModifiedBefore<=AllowedBeforeDecimal)
  214.             {
  215.               movmem((Value+CurrentLocation),(Value+CurrentLocation+1),
  216.                 strlen(Value)-CurrentLocation);
  217.               *(Value+CurrentLocation)='.';
  218.               CurrentLocation++;
  219.             }
  220.           }
  221.         }
  222.       }
  223.       return CompleteEvent;
  224.  
  225.     case kbBackSpace:
  226.       EditOverriden++;
  227.  
  228.       if (!CurrentLocation)
  229.         return CompleteEvent;
  230.  
  231.       if (!*(Value+CurrentLocation))
  232.       {
  233.         *(Value+(--CurrentLocation))=0;
  234.       }
  235.       else if (CurrentLocation==strlen(Mask)-1)
  236.       {
  237.         *(Value+CurrentLocation)=0;
  238.       }
  239.       else
  240.       {
  241.         if (*((Value+CurrentLocation)-1)=='.' &&
  242.           ActualBeforeDecimal+ActualAfterDecimal>AllowedBeforeDecimal)
  243.           return CompleteEvent;
  244.  
  245.         movmem((Value+CurrentLocation),(Value+CurrentLocation-1),
  246.           (strlen(Value)-CurrentLocation)+1);
  247.  
  248.         CurrentLocation--;
  249.       }
  250.  
  251.       return CompleteEvent;
  252.  
  253.     case kbDel:
  254.       EditOverriden++;
  255.  
  256.       if (!*(Value+CurrentLocation))
  257.         return CompleteEvent;
  258.  
  259.       if (*(Value+CurrentLocation)=='-' || *(Value+CurrentLocation)>='0')
  260.       {
  261.         movmem((Value+CurrentLocation+1),(Value+CurrentLocation),
  262.           (strlen(Value)-CurrentLocation)+1);
  263.       }
  264.       else
  265.       {
  266.         if (ActualBeforeDecimal+ActualAfterDecimal<=AllowedBeforeDecimal)
  267.         {
  268.           movmem((Value+CurrentLocation+1),(Value+CurrentLocation),
  269.             (strlen(Value)-CurrentLocation)+1);
  270.         }
  271.       }
  272.  
  273.       return CompleteEvent;
  274.  
  275.     case '-':
  276.       if (AllowedNegative && NoEditErase && !EditOverriden)
  277.       {
  278.         EditOverriden++;
  279.         *(Value+1)=0;
  280.         CurrentLocation=0;
  281.         ActualBeforeDecimal=0;
  282.         ActualAfterDecimal=0;
  283.       }
  284.  
  285.       if (AllowedNegative && !CurrentLocation && *Value!='-')
  286.       {
  287.         if (InsertMode)
  288.         {
  289.           movmem((Value+CurrentLocation),(Value+CurrentLocation+1),
  290.             (strlen(Value)-CurrentLocation)+1);
  291.           CurrentLocation++;
  292.           *Value='-';
  293.         }
  294.         else
  295.         {
  296.           if (*Value=='.' && ActualAfterDecimal>=AllowedBeforeDecimal)
  297.             return CompleteEvent;
  298.  
  299.           *Value='-';
  300.           CurrentLocation++;
  301.         }
  302.         return CompleteEvent;
  303.       }
  304.  
  305.     default:
  306.       if (!AllowedBeforeDecimal && !AllowedAfterDecimal)
  307.         return CompleteEvent;
  308.  
  309.       if (Event>='0' && Event<='9')
  310.       {
  311.         if (!strchr(Value,'.') && !AllowedBeforeDecimal)
  312.           return CompleteEvent;
  313.  
  314.         if (NoEditErase && !EditOverriden)
  315.         {
  316.           EditOverriden++;
  317.           *Value=0;
  318.           CurrentLocation=0;
  319.           ActualBeforeDecimal=0;
  320.           ActualAfterDecimal=0;
  321.         }
  322.  
  323.         if (!InsertMode)
  324.         {
  325.  
  326. NoInsert:
  327.  
  328.           // ---------------------------------------------------------------
  329.           // typing over a number
  330.           // ---------------------------------------------------------------
  331.  
  332.           if (*(Value+CurrentLocation)>='0' && *(Value+CurrentLocation)<='9')
  333.           {
  334.             *(Value+CurrentLocation++)=Event;
  335.             return CompleteEvent;
  336.           }
  337.  
  338.           // ---------------------------------------------------------------
  339.           // typing at end of number - checks to make sure before and after
  340.           // decimal places are not too many to fit in the mask
  341.           // ---------------------------------------------------------------
  342.  
  343.           if (!*(Value+CurrentLocation))
  344.           {
  345.             if ((!strchr(Value,'.') && ActualBeforeDecimal<AllowedBeforeDecimal)
  346.              || (strchr(Value,'.') && ActualAfterDecimal<AllowedAfterDecimal))
  347.             {
  348.               *(Value+CurrentLocation++)=Event;
  349.               *(Value+CurrentLocation)=0;
  350.             }
  351.             return CompleteEvent;
  352.           }
  353.  
  354.           // ---------------------------------------------------------------
  355.           // typing over a negative sign
  356.           // ---------------------------------------------------------------
  357.  
  358.           if (*(Value+CurrentLocation)=='-')
  359.           {
  360.             if ((!ActualBeforeDecimal && AllowedBeforeDecimal)
  361.               || (ActualBeforeDecimal && ActualBeforeDecimal<AllowedBeforeDecimal))
  362.             {
  363.               *(Value+CurrentLocation++)=Event;
  364.             }
  365.             return CompleteEvent;
  366.           }
  367.  
  368.           // ---------------------------------------------------------------
  369.           // typing over a decimal point
  370.           // ---------------------------------------------------------------
  371.  
  372.           if (*(Value+CurrentLocation)=='.')
  373.           {
  374.             if (ActualBeforeDecimal+ActualAfterDecimal<AllowedBeforeDecimal)
  375.             {
  376.               *(Value+CurrentLocation++)=Event;
  377.             }
  378.             return CompleteEvent;
  379.           }
  380.         }
  381.         else
  382.         {
  383.           if ((!CurrentLocation && !*Value) || !*(Value+CurrentLocation))
  384.             goto NoInsert;
  385.  
  386.           if (*(Value+CurrentLocation)=='-')
  387.             return CompleteEvent;
  388.  
  389.           // ---------------------------------------------------------------
  390.           // insertion prior to decimal
  391.           // ---------------------------------------------------------------
  392.  
  393.           if (strchr(Value,'.') && (Value+CurrentLocation)<=strchr(Value,'.'))
  394.           {
  395.             if (ActualBeforeDecimal<AllowedBeforeDecimal)
  396.             {
  397.               movmem((Value+CurrentLocation),(Value+CurrentLocation+1),
  398.                 (strlen(Value)-CurrentLocation)+1);
  399.               *(Value+CurrentLocation)=Event;
  400.               CurrentLocation++;
  401.             }
  402.             return CompleteEvent;
  403.           }
  404.  
  405.           // ---------------------------------------------------------------
  406.           // insertion after to decimal
  407.           // ---------------------------------------------------------------
  408.  
  409.           if (strchr(Value,'.') && (Value+CurrentLocation)>strchr(Value,'.'))
  410.           {
  411.             if (ActualAfterDecimal<AllowedAfterDecimal)
  412.             {
  413.               movmem((Value+CurrentLocation),(Value+CurrentLocation+1),
  414.                 (strlen(Value)-CurrentLocation)+1);
  415.               *(Value+CurrentLocation)=Event;
  416.               CurrentLocation++;
  417.             }
  418.             return CompleteEvent;
  419.           }
  420.  
  421.           // ---------------------------------------------------------------
  422.           // insertion without a decimal
  423.           // ---------------------------------------------------------------
  424.  
  425.           if (ActualBeforeDecimal<AllowedBeforeDecimal)
  426.           {
  427.             movmem((Value+CurrentLocation),(Value+CurrentLocation+1),
  428.               (strlen(Value)-CurrentLocation)+1);
  429.             *(Value+CurrentLocation)=Event;
  430.             CurrentLocation++;
  431.             return CompleteEvent;
  432.           }
  433.         }
  434.       }
  435.       break;
  436.   }
  437.  
  438.   return Event;
  439. }
  440.  
  441. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  442. //
  443. // TrimTrailingZeros()
  444. //
  445. // Slice off 0's at end of number (i.e. before: 10.5000 after: 10.5)
  446. // Removes decimal place if it isn't needed
  447. // Removes dead zeros (i.e. before: -0000 after: NULL)
  448. //
  449. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  450.  
  451. void Numerics::TrimTrailingZeros()
  452. {
  453.   if (!*Value)
  454.     return;
  455.  
  456.   // scan entire number and see if any NON-ZERO numbers appear -- if none
  457.   // appear, just turn the # into a zero
  458.  
  459.   if (!*(Value+strcspn(Value,"123456789")))
  460.   {
  461.     *Value=0;
  462.     return;
  463.   }
  464.  
  465.   // if all else fails ...
  466.  
  467.   if (strchr(Value,'.'))
  468.   {
  469.     char *Current=(Value+strlen(Value)-1);
  470.  
  471.     do
  472.     {
  473.       if (*Current=='0')
  474.         *Current=0;
  475.       else if (*Current=='.')
  476.       { // if nada behind decimal, knock off decimal
  477.         *Current=0;
  478.         break;
  479.       }
  480.       else
  481.         break;
  482.  
  483.       Current--;
  484.     }
  485.     while (Current>=Value);
  486.   }
  487.  
  488.   if (*Value && !strchr(Value,'.'))
  489.   {
  490.     char *AltValue=Value;
  491.     int Counter=0;
  492.  
  493.     do
  494.     {
  495.       if (*AltValue>'0' && *AltValue<='9')
  496.         Counter++;
  497.     } while(*++AltValue);
  498.  
  499.     if (!Counter)
  500.     {
  501.       *Value=0;
  502.       CurrentLocation=0;
  503.     }
  504.   }
  505.  
  506.   if (strchr(Value,' '))     // get rid of white space at END of string
  507.     *(strchr(Value,' '))=0;  // only occurs on floating point based numbers
  508. }
  509.  
  510.